if (!com) var com = {}
if (!com.logicpartners) com.logicpartners = {}
if (!com.logicpartners.designerTools) com.logicpartners.designerTools = {}

com.logicpartners.designerTools.text = function () {
  var self = this
  this.counter = 1
  this.button = $('<div></div>')
    .addClass('designerToolbarText designerToolbarButton')
    .attr('title', 'Text')
    .append($('<div></div>'))

  this.object = function (x, y, width, height) {
    this.toolName = 'text'
    this.props = {
      type: { label: '类型', default: '文本' },
      name: { label: '元素', default: 'Textbox ' + self.counter++ },
      contentSet: {
        label: '内容设置',
        default: 'fixedContent',
        options: [
          { label: '固定内容', value: 'fixedContent' },
          { label: '数据来源', value: 'dataSource' },
        ],
      },
      text: { label: '内容', default: 'Textbox' },
      dataSourcefield: { label: '来源字段', default: 'Textbox' },
      // dataSourcefield: {
      //   label: '来源字段',
      //   default: 'noValue',
      //   options: [
      //     { label: '请选择', value: 'noValue' },
      //     { label: '请选择1', value: 'noValue1' },
      //     // { label: '固定内容a', value: 'fixedContent' },
      //     // { label: '数据来源b', value: 'dataSource' },
      //   ],
      // },
      x: { label: '起始位置x(mm)', default: x },
      y: { label: '起始位置y(mm)', default: y },
      width: { label: '宽度(mm)', default: 30 },
      height: { label: '高度(mm)', default: 8 },
      fontSize: { label: '字体大小', default: 8 },
      fontType: { label: '字体类型', default: 'Arial' },
      ZindexTop: { label: '置顶', button: '置顶' },
      ZindexBottom: { label: '置底', button: '置底' },
      ZindexUp: { label: '上移一层', button: '上移一层' },
      ZindexDown: { label: '下移一层', button: '下移一层' },
    }
    for (var prop in this.props) {
      this[prop] = this.props[prop].default
    }
    this.readonly = ['type', 'name', 'width', 'height', 'dataSourcefield']

    // if (this.contentSet == 'fixedContent') {
    // } else {
    //   this.readonly = ['type', 'name', 'width', 'height']
    // }

    this.setWidth = function (width) {
      if (this.readonly && this.readonly.includes('width')) return
      this.width = precisionNumber(width)
    }

    this.getWidth = function () {
      return this.width
    }

    this.setHeight = function (height) {
      if (this.readonly && this.readonly.includes('height')) return
      this.height = precisionNumber(height)
    }

    this.getHeight = function () {
      return this.height
    }

    // 疑问
    this.hitTest = function (coords) {
      return (
        coords.x >= this.x && coords.x <= this.x + this.width && coords.y >= this.y && coords.y <= this.y + this.height
      )
    }

    // this.getFontHeight = function () {
    //   var textMeasure = $('<div></div>')
    //     .css({
    //       'font-size': this.fontSize * 10 + 'px',
    //       'font-family': this.fontType,
    //       'line-height': '1',
    //       opacity: 0,
    //     })
    //     .text('M')
    //     .appendTo($('body'))

    //   var height = textMeasure.outerHeight() / 10
    //   textMeasure.remove()
    //   return height
    // }

    this.draw = function (context) {
      // this.props.text.default = this.text
      // console.log(this.props.text.default, 'this1')
      // console.log(this.text, 'this2')
      // console.log(this.text == this.props.text.default)

      this.props.text.default = this.text
      this.x = precisionNumber(this.x)
      this.y = precisionNumber(this.y)
      context.font = this.fontSize + 'px ' + this.fontType
      var measuredText = context.measureText(this.text)
      this.width = precisionNumber(measuredText.width)
      this.height = precisionNumber(this.fontSize)
      context.textBaseline = 'top'
      context.fillText(this.text, this.x, this.y)
    }

    this.drawActive = function (context) {
      context.dashedStroke(this.x, this.y, this.x + this.width, this.y + this.height, [1, 0.5])
    }

    // 保存
    this.toZPL = function (labelwidth, labelheight) {
      if (this.contentSet == 'fixedContent') {
        return `^FO${this.x},${this.y}^A1N,${this.fontSize},${this.fontSize},^FD${this.text}^FS`
      } else {
        return `^FO${this.x},${this.y}^A1N,${this.fontSize},${this.fontSize},^FD{${this.dataSourcefield}}^FS`
      }
      // console.log(`^FO${this.x},${this.y}^A0,${this.fontSize},${this.fontSize},^FD${this.text}^FS`, '2222222')
    }
  }
}
